import axios from 'axios'
import { getToken, removeToken, setToken } from './cookie'
import router from '../router'
import { Message } from 'element-ui'
const consoleURL = process.env.VUE_APP_7_OPEN
const baseURL = process.env.VUE_APP_BASE_API
const buildPlatformTestObj = process.env.VUE_APP_X_Account
// var baseURL = '/index.php'
const service = axios.create({
  baseURL,
  timeout: 100000 * 50, // 请求超时时间
  withCredentials: true,
  headers: {
    'X-Server-Name': 'tyun'
  }
})

// request拦截,在请求发起前搞事情
service.interceptors.request.use((config) => {
  //config.data.UserID = 14320;
  config.headers['X-Server-Name'] = 'tyun';
  if (process.env.NODE_ENV === 'development') {
	  //  setToken('IMOrbrv4EQjf3G65iwPax4sT37PKB8Dc')
  }
  if (getToken()) {
    config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  config.headers['X-Account'] = buildPlatformTestObj;//建平台开发测试使用

  if (config.contentType == 'json')
    config.headers['Content-Type'] = 'application/json';
    
  return config
})

// respone拦截,获取请求数据后，搞事情
service.interceptors.response.use(
  // 正常获取数据

  response => {
    if (response.headers['content-type'] != null && (response.headers['content-type'].indexOf('vnd.ms-excel') > 0 || response.headers['content-type'].indexOf('spreadsheetml.sheet') > 0 )) {
      return response
    }
    if (response.data && response.data.code == 200) {
      return response.data
    } else if (response.data.code === 403) {
      // 抱歉，你无权访问该页面
      // router.push('/403')
      console.log(response)
      window.location.href = consoleURL + '403'
    } else if (response.data && response.data.code == 201) {
      return Promise.reject(response)
    } else if (response.data && response.data.code == 502) {
      return Promise.reject(response)
    }
    else {
      if (response.request.responseType == 'blob') {
        Message({ type: 'error', message: "没有权限访问，或者登录信息失效" })
      } else if (response.data.code === 401) {
        removeToken('X-Token')
        // console.log("jump", response)
        // router.push("/login/login")
        window.location.href = consoleURL + "login/login?backUrl=" + escape(window.location.href)
      } else if (response.data.code > 200 && response.data.code < 300) {
        // 不显示报错弹框
        if (response.data.code === 203) {
          console.log(203, response, store)
          if (store.state.user.tel) {
            var msg = response.data.message.replace("[mobile]", store.state.user.tel)
            Message({ type: 'error', message: msg })
            return Promise.reject(response)
          }
        }
      }
      else {
        let url = response.config.url
        let baseURL = response.config.baseURL
        let apiURL = url.replace(baseURL, '')
        // 当请求的接口是【获取可解析的域名】或者【获取实名认证】的时候，不需要弹框
        if (apiURL !== 'aggregateservice-web/api/User_Domain/GetAnalysisDomainListNew' && apiURL !== 'tcloud-account/authentication/selectAuthenticationInfoStatus' && apiURL !== 'tcloud-account/account/checkUserFirstLogin') {
          Message({ type: 'error', message: response.data.message })
        }
      }
      return Promise.reject(response)
    }
  },
  // 请求失败
  error => {
    console.log('请求错误', error)
    return Promise.reject('error', error)
  },
)

export default service
